package online.xuping.thousandsOfWorlds.thousandsOfWorlds.service;

import cn.hutool.json.JSONUtil;
import com.github.artbits.quickio.api.Collection;
import com.github.artbits.quickio.core.IOEntity;
import lombok.Data;

import java.util.List;


public class ServiceImpl<T extends IOEntity> extends BaseService<T> implements IMapService{


    public ServiceImpl(Class<T> genericClass,String dbName ) {
        super(genericClass);
        setDbName(dbName);
        initDb();

    }

    @Override
    public List<T> findAll() {
        Collection<T>  c=getDb().collection(getGenericClass() );
        return    c.findAll( );
    }


    @Override
    public Object newDomain(Object o) {
        Collection<T>  c=getDb().collection(getGenericClass());
        c.save((T)o);
        return o;
    }

    @Override
    public T findById(Long id) {
        Collection<T>  c=getDb().collection(getGenericClass() );
        T re =    c.findWithIndex(options -> options.index(indexName(), id));
        return re;
    }

    @Override
    public Object updateById(Object o) {
        Collection<T>  c=getDb().collection(getGenericClass());
        c.updateWithIndex((T)o,options -> options.index(indexName(), JSONUtil.parseObj(o).getLong("id")));
        return o;
    }


    @Override
    public int delByMapId(Long id) {
        Collection<T>  c=getDb().collection(getGenericClass());
        c.deleteWithIndex(options -> options.index(indexName(), id));
        return 1;
    }
}
